import homepage from '@/api/homepage'
import * as types from '../mutation-types'

const state = {
    banners: [],
    recommendGoods: [],
    hotGoods: [],
    requestSend: false
}

const getters = {
    banners: state => state.banners,
    recommendGoods: state => state.recommendGoods,
    hotGoods: state => state.hotGoods
}

const actions = {
    async getBanners ({commit}) {
        if (state.requestSend) return
        const banners = await homepage.getBanners()
        commit(types.GET_BANNERS, {banners})
    },
    async getHotGoods ({commit}) {
        if (state.requestSend) return
        const data = await homepage.getHotGoods()
        commit(types.GET_HOT_GOODS, data)
    }
}

const mutations = {
    [types.GET_BANNERS] (state, {banners}) {
        state.banners = banners
    },
    [types.GET_HOT_GOODS] (state, {rec, hotgoods}) {
        state.recommendGoods = rec
        state.hotGoods = hotgoods
        state.requestSend = true
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
